//
// Created by steven on 2017/2/24.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMComboListViewController.h"
#import "GMComboTableViewCell.h"
#import "GMOrderListViewController.h"
#import "GMComboDetailViewController.h"
#import "GMOrderDataController.h"
#import <MJRefresh/MJRefresh.h>

#define kComboTableViewCellIdentifier @"comboTableViewCellIdentifier"

@interface GMComboListViewController()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)GMOrderDataController *orderDataController;
@property (nonatomic, strong)NSMutableArray *combos;

@end

@implementation GMComboListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.orderDataController = [[GMOrderDataController alloc] init];
    self.combos = [[NSMutableArray alloc] init];

    [self setupMainView];
    [self setMJRefresh];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIBarButtonItem *buyBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"我的订单"
                                                                         style:UIBarButtonItemStylePlain
                                                                        target:self
                                                                        action:@selector(buyBarButtonItemPressed)];
    self.navigationItem.rightBarButtonItem = buyBarButtonItem;
    [self refreshCombos];
}

- (void)setupMainView {
    self.navigationItem.title = @"购买套餐";
    [self.view addSubview:self.tableView];
    [self.tableView autoPinEdgesToSuperviewEdges];
}

#pragma mark - set MJRefresh
- (void)setMJRefresh {

    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if(weakSelf) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf refreshCombos];
        }
    }];

    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (weakSelf) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf loadMoreCombos];
        }
    }];
    self.tableView.mj_footer.automaticallyHidden = YES;
}

#pragma mark - refresh combos
- (void)refreshCombos {
    NSDictionary *params = @{@"page":@"1", @"perpage":PER_PAGE_RECORD};
    [self requestCombosWithParams:params];
}

#pragma mark - load more combos
- (void)loadMoreCombos {
    NSInteger currentPage = self.orderDataController.comboRecord.currentPage;
    NSInteger totalPage = self.orderDataController.comboRecord.totalPage;
    if (currentPage < totalPage) {
        NSDictionary *params = @{@"page":[NSString stringWithFormat:@"%ld", currentPage+1],@"perpage":PER_PAGE_RECORD};
        [self requestCombosWithParams:params];
    } else {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
}

#pragma mark - request combos
- (void)requestCombosWithParams:(NSDictionary *)params {
    [self.orderDataController requestCombosWithParams:params callback:^(NSString *error) {
        if (error == nil) {
            [self renderSuccessView];
        } else {
            [self showAlertMessage:error];
        }
    }];
}

#pragma mark - get combos success
- (void)renderSuccessView {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];

    self.combos = self.orderDataController.comboRecord.combos;
    [self.tableView reloadData];
}

#pragma mark - buy success
- (void)renderBuySuccessView {
    [self showAlertMessage:@"购买成功！"];
}

- (void)buyBarButtonItemPressed {
    GMOrderListViewController *orderViewController = [[GMOrderListViewController alloc] init];
    [self.navigationController pushViewController:orderViewController animated:YES];
}

#pragma mark - comboTableView
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[GMComboTableViewCell class] forCellReuseIdentifier:kComboTableViewCellIdentifier];
    }

    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.combos.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GMComboTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kComboTableViewCellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[GMComboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kComboTableViewCellIdentifier];
    }

    GMComboModel *comboModel = self.combos[indexPath.row];
    
    cell.countLabel.text = [NSString stringWithFormat:@"%@", comboModel.quantity];
    cell.scoreLabel.text = [NSString stringWithFormat:@"购买可获得%@积分", comboModel.score];
    // TODO
    // cell.titleLabel.text = comboModel.name;
    cell.titleLabel.text = @"可用于守卫五名用户或车辆";
    cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", comboModel.price];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 104;
}

#pragma mark - UITableViewDelegate
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    GMComboModel *comboModel = self.combos[indexPath.row];
    GMComboDetailViewController *comboDetailViewController = [[GMComboDetailViewController alloc] init];
    comboDetailViewController.comboModel = comboModel;
    [self.navigationController pushViewController:comboDetailViewController animated:YES];
}

@end
